<template>
  <div>
    <p>Header</p>
    <p>{{ count }}</p>
    <button @click="add" class="right">+1</button>
    <button @click="sub" class="right">-1</button>
    <input
      type="text"
      placeholder="请输入数字"
      v-model.number="num"
      class="right"
      @keydown.enter="addArb"
    />
    <button @click="Clear" class="right">清零</button>
    <button @click="num = null" class="right">清空输入框</button>
    <button @click="addArb">增加你想的值</button>
  </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
export default {
  name: "Header",
  data() {
    return {
      num: null,
    };
  },
  computed: {
    // 引入state数据 第一个参数是数据的来源模块
    ...mapState("data", ["count"]),
  },
  methods: {
    ...mapMutations("data", ["add", "sub"]),
    addArb() {
      // 通过这种方式获取数据时需要在执行的函数前加模块名
      this.$store.commit("data/addArb", { a: this.num });
    },
    Clear() {
      this.$store.commit("data/Clear");
      this.num = null;
    },
  },
};
</script>

<style>
.right {
  margin-right: 10px;
}
</style>